#include "Cube_Step.h"

uint8_t U1_Restore_Formula[64] = "R1L1F2B2R\'L\'D1R1L1F2B2R\'L\'";
uint8_t UC_Restore_Formula[64] = "R1L1F2B2R\'L\'D\'R1L1F2B2R\'L\'";
uint8_t U2_Restore_Formula[64] = "R1L1F2B2R\'L\'D2R1L1F2B2R\'L\'";

extern Motors *motor;
//顶面层转
void U1_Restore()
{
    int i = 0;
    while (U1_Restore_Formula[2 * i] != '\0')
    {
        Auto_Machine(U1_Restore_Formula[2 * i], U1_Restore_Formula[2 * i + 1]);
        i++;
    }
}

void UC_Restore()
{
    int i = 0;
    while (UC_Restore_Formula[2 * i] != '\0')
    {
        Auto_Machine(UC_Restore_Formula[2 * i], UC_Restore_Formula[2 * i + 1]);
        i++;
    }
}

void U2_Restore()
{
    int i = 0;
    while (U1_Restore_Formula[2 * i] != '\0')
    {
        Auto_Machine(U2_Restore_Formula[2 * i], U2_Restore_Formula[2 * i + 1]);
        i++;
    }
}
//底面层转
void D1_Restore()
{
    clockwise_90(motor->Down);
}

void DC_Restore()
{
    counterclockwise_90(motor->Down);
}

void D2_Restore()
{
    clockwise_180(motor->Down);
}
//前面层转
void F1_Restore()
{
    clockwise_90(motor->Face);
}

void FC_Restore()
{
    counterclockwise_90(motor->Face);
}

void F2_Restore()
{
    clockwise_180(motor->Face);
}
//背面层转
void B1_Restore()
{
    clockwise_90(motor->Back);
}

void BC_Restore()
{
    counterclockwise_90(motor->Back);
}

void B2_Restore()
{
    clockwise_180(motor->Back);
}
//右面层转
void R1_Restore()
{
    clockwise_90(motor->Right);
}

void RC_Restore()
{
    counterclockwise_90(motor->Right);
}

void R2_Restore()
{
    clockwise_180(motor->Right);
}
//左面层转
void L1_Restore()
{
    clockwise_90(motor->Left);
}

void LC_Restore()
{
    counterclockwise_90(motor->Left);
}

void L2_Restore()
{
    clockwise_180(motor->Left);
}

//旋转魔方整体
void Rotate_Cube()
{
    // counterclockwise_90(motor->Down);
    int i;
    HAL_GPIO_WritePin(motor->Down.Direct_GPIO, motor->Down.Direct_GPIO_PIN, GPIO_PIN_RESET);
    for (i = 0; i < Cycle_90; i++)
    {
        HAL_GPIO_TogglePin(motor->Down.Pull_GPIO, motor->Down.Pull_GPIO_PIN);
        delay_us(200);
        HAL_GPIO_TogglePin(motor->Down.Pull_GPIO, motor->Down.Pull_GPIO_PIN);
        delay_us(200);
    }
    HAL_Delay(stop_time);
}
void CounterRotate_Cube()
{
    // clockwise_90(motor->Down);
    int i;
    HAL_GPIO_WritePin(motor->Down.Direct_GPIO, motor->Down.Direct_GPIO_PIN, GPIO_PIN_SET);
    for (i = 0; i < Cycle_90; i++)
    {
        HAL_GPIO_TogglePin(motor->Down.Pull_GPIO, motor->Down.Pull_GPIO_PIN);
        delay_us(200);
        HAL_GPIO_TogglePin(motor->Down.Pull_GPIO, motor->Down.Pull_GPIO_PIN);
        delay_us(200);
    }
    HAL_Delay(stop_time);
}

void Lift_Cube()
{
    int i = 0;
    for (i = 0; i < (High / High_Cycle); i++)
        counterclockwise_360(motor->Lift);
    T_flag = 1;
}

void CounterLift_Cube()
{
    int i = 0;
    for (i = 0; i < (High / High_Cycle); i++)
        clockwise_360(motor->Lift);
    T_flag = 0;
}
